{% extends 'index.html' %}
{% block ace-content %} 
<style type="text/css">
	.row {
		margin-right: -15px;
  		margin-left: -15px;
	}
</style>
{% endblock %}
{% block page-content %}
<div id="page-wrapper">
    <div class="row">
         <div class="col-lg-12">
              <h1 class="page-header"><i class="fa  fa-align-justify "></i> 工单列表</h1>
         </div>
                <!-- /.col-lg-12 -->
         
    </div>
     <div class="row">
     	<div class="col-lg-8">
			<div class="panel panel-default">
				<div class="panel-heading">
				<i class="fa fa-bar-chart-o fa-fw"></i> 月度工单更新统计
				</div>
				<!-- /.panel-heading -->
				<div class="panel-body">
					<div class="row">
						<div class="col-lg-4">
							<div class="table-responsive">
								<table class="table table-bordered table-hover table-striped">
									<thead>
										<tr>
											<th>#</th>
											<th class="text-center">类型</th>
											<th class="text-center">次数</th>
										</tr>
									</thead>
									<tbody>
										{% for ds in orderType %}
										<tr>
											<td>{{ forloop.counter }}</td>
											<td class="text-center">{% if ds.order_type == 1 %}代码部署{% elif ds.order_type == 0 %}SQL更新{% elif ds.order_type == 2  %}文件分发{% elif ds.order_type == 3  %}文件下载{% endif  %}</td>
											<td class="text-center">{{ ds.dcount }}</td>
										</tr>
										{% endfor %}
									</tbody>
								</table>
							</div>
							<!-- /.table-responsive -->
						</div>
						<!-- /.col-lg-4 (nested) -->
						<div class="col-lg-8">
							<div id="morris-bar-chart-total"></div>
						</div>
						<!-- /.col-lg-8 (nested) -->
					</div>
					<!-- /.row -->
				</div>
				<!-- /.panel-body -->
			</div>  
		</div>  
                <div class="col-lg-4">
                    <div class="panel panel-default">
                        <div class="panel-heading">
                            	工单状态统计
                        </div>
                        <!-- /.panel-heading -->
                        <div class="panel-body">
                            <div id="morris-donut-chart"></div>
                        </div>
                        <!-- /.panel-body -->
                    </div>
                    <!-- /.panel -->
                </div>  		
     </div> 
     <div class="row">
         <div class="col-lg-6">
             <div class="panel panel-default">
                 <div class="panel-heading">
                     	<i class="fa fa-bar-chart-o fa-fw"></i> 代码部署统计
                 </div>
                 <!-- /.panel-heading -->
                 <div class="panel-body">
                     <div id="morris-area-chart-code"></div>
                 </div>
                 <!-- /.panel-body -->
             </div>
             <!-- /.panel -->
         </div>
         <!-- /.col-lg-6 -->
         <div class="col-lg-6">
             <div class="panel panel-default">
                 <div class="panel-heading">
                     <i class="fa fa-bar-chart-o fa-fw"></i> SQL更新统计
                 </div>
                 <!-- /.panel-heading -->
                 <div class="panel-body">
                     <div id="morris-bar-chart-sql"></div>
                 </div>
                 <!-- /.panel-body -->
             </div>
             <!-- /.panel -->
         </div>
        </div>         
    <div class="row">
         <div class="col-lg-12">
                    <div class="panel panel-default">
                        <div class="panel-heading">
                            	<i class="fa fa-database"></i>工单列表明细 
                           	
                        </div>
                        <!-- /.panel-heading -->
                        <div class="panel-body">                   
                            <table width="100%" class="table table-striped table-bordered table-hover dataTable no-footer" id="deployTableList">
                                <thead>
                                    <tr>
                                    	<th class="text-center">ID</th>
                                    	<th class="text-center">工单类型</th>
                                    	<th class="text-center">工单申请人</th>
                                    	<th class="text-center">工单主题</th>
                                    	<th class="text-center">工单内容</th>
                                        <th class="text-center">执行人</th>
                                        <th class="text-center">发布时间</th>
                                        <th class="text-center">工单状态</th>
                                        <th class="text-center">操作</th>
                                    </tr>
                                </thead>
                                <tbody>
                                	{% for ds in orderList %}
                                		<tr>
                                			<td class="text-center">{{ds.id}} </td>
											<td class="text-center">
												{% if ds.order_type == 1 %}
													<span class="label label-success">代码更新</span>
												{% elif ds.order_type == 2 %}
													<span class="label label-warning">文件分发</span>
												{% elif ds.order_type == 0 %}
													<span class="label label-info">SQL更新</span>
												{% elif ds.order_type == 3 %}
													<span class="label label-danger">文件下载</span>												
												{% endif %}
                                			</td>  
                                			<td class="text-center">
												{{ds.order_user}}
                                			</td>                              			
                                			<td class="text-center">
                                				{{ds.order_subject}}
                                			</td>
                                			<td class="text-center">
                                				<a href="{{ds.order_url}}" target="_blank" class="tooltip-test" data-toggle="tooltip" title="{{ds.order_content}}">{{ds.order_content|slice:"50"}}</a>
                                			</td>                                			
                                			<td class="text-center">{{ds.order_executor}}</td>
                                			<td class="text-center">{{ds.create_time|date:"Y/m/d H:i  "}}</td>
                                			<td class="text-center">
                                				{% if ds.order_status == 0 %}
                                					<span class="label label-success">已通过</span>                                			
                                				{% elif ds.order_status == 1 %}
                                					<span class="label label-danger">已拒绝</span>
                                				{% elif ds.order_status == 2 %}
                                					<span class="label label-info">审核中</span>
                                				{% elif ds.order_status == 3 %}
                                					<span class="label label-success">已部署</span>  
                                				{% elif ds.order_status == 4 %}
                                					<span class="label label-info">待授权</span>     
                                				{% elif ds.order_status == 5 %}
                                					<span class="label label-success">已执行</span>                                 					                            					
                                				{% elif ds.order_status == 6 %}
                                					<span class="label label-default">已回滚</span>     
                                				{% elif ds.order_status == 7 %}
                                					<span class="label label-danger">已撤回</span>   
                                				{% elif ds.order_status == 8 %}
                                					<span class="label label-warning">已授权</span>                                  					                             					                         					
                                				{% else %}
                                					<span class="label label-danger">已失败</span>
                                				{% endif %}
                                			</td>
                                			<td class="text-center">  
                                				{% if user.is_superuser %}
                                					{% if ds.order_type == 1 and ds.order_status == 3 %}                               						
                                						<a href="/deploy_order/rollback/{{ds.id}}/" target="_blank"><button  type="button" class="btn btn-default"><abbr title="回滚代码"><i class="fa fa-play-circle-o"></i></button></a>	
                                					{% elif ds.order_type == 1  %}	
                                						<a href="/deploy_order/status/{{ds.id}}/" target="_blank"><button  type="button" class="btn btn-default"><abbr title="部署代码"><i class="fa fa-play-circle-o"></i></button></a>	
                                					{% elif ds.order_type == 0 %}
                                						<a href="/db/sql/order/run/{{ds.id}}/" target="_blank"><button  type="button" class="btn btn-default"><abbr title="更新SQL"><i class="fa fa-play-circle-o"></i></button></a>	
                                					{% elif ds.order_type == 2 %}
                                						<a href="/file/upload/run/{{ds.id}}/" target="_blank"><button  type="button" class="btn btn-default"><abbr title="文件分发"><i class="fa fa-play-circle-o"></i></button></a>	    
                                					{% elif ds.order_type == 3 %}
                                						<a href="/file/download/run/{{ds.id}}/" target="_blank"><button  type="button" class="btn btn-default"><abbr title="文件下载"><i class="fa fa-play-circle-o"></i></button></a>                                						                            						
                                					{% endif %}													
													{% if ds.order_status == 4 or ds.order_status == 2 %}
														<button  type="button" class="btn btn-default" onclick="updateOrderStatus(this,{{ds.id}},'auth')"><abbr title="授权"><i class="fa fa-check"  ></i></button>
														<button  type="button" class="btn btn-default" onclick="updateOrderStatus(this,{{ds.id}},'disable')"><abbr title="取消"><i class="fa fa-times"></i></button>
													{% elif ds.order_status == 8 %}
														<button  type="button" class="btn btn-default disabled"><abbr title="授权"><i class="fa fa-check"></i></button>	
														<button  type="button" class="btn btn-default disabled"><abbr title="取消"><i class="fa fa-times "></i></button>
													{% elif ds.order_status == 5 %}
														<button  type="button" class="btn btn-default disabled"><abbr title="授权"><i class="fa fa-check"></i></button>
														<button  type="button" class="btn btn-default disabled"><abbr title="取消"><i class="fa fa-times "></i></button>											
													{% else %}
														<button  type="button" class="btn btn-default disabled"><abbr title="授权"><i class="fa fa-check"></i></button>
														<button  type="button" class="btn btn-default" onclick="updateOrderStatus(this,{{ds.id}},'disable')"><abbr title="取消"><i class="fa fa-times "></i></button>		
													{% endif %}														
													<button  type="button" class="btn btn-default" onclick="deleteOrder(this,{{ds.id}})"><abbr title="删除"><i class="glyphicon glyphicon-trash"></i></button>
												{% else %}
													{% if ds.perm %}
	                           							{% if ds.order_type == 1 and ds.order_status == 3 %}                               						
	                                						<a href="/deploy_order/rollback/{{ds.id}}/" target="_blank"><button  type="button" class="btn btn-default"><abbr title="回滚代码"><i class="fa fa-play-circle-o"></i></button></a>	
	                                					{% elif ds.order_type == 1  %}	
	                                						<a href="/deploy_order/status/{{ds.id}}/" target="_blank"><button  type="button" class="btn btn-default"><abbr title="部署代码"><i class="fa fa-play-circle-o"></i></button></a>	
	                                					{% elif ds.order_type == 0 %}
	                                						<a href="/db/sql/order/run/{{ds.id}}/" target="_blank"><button  type="button" class="btn btn-default"><abbr title="更新SQL"><i class="fa fa-play-circle-o"></i></button></a>	
	                                					{% elif ds.order_type == 2 %}
	                                						<a href="/file/upload/run/{{ds.id}}/" target="_blank"><button  type="button" class="btn btn-default"><abbr title="文件分发"><i class="fa fa-play-circle-o"></i></button></a>	
                                						{% elif ds.order_type == 3 %}
                                						<a href="/file/download/run/{{ds.id}}/" target="_blank"><button  type="button" class="btn btn-default"><abbr title="文件下载"><i class="fa fa-play-circle-o"></i></button></a> 	                                						                                						
	                                					{% endif %}		
														{% if ds.order_status == 4 or ds.order_status == 2 %}
															<button  type="button" class="btn btn-default" onclick="updateOrderStatus(this,{{ds.id}},'auth')"><abbr title="授权"><i class="fa fa-check"  ></i></button>
															<button  type="button" class="btn btn-default" onclick="updateOrderStatus(this,{{ds.id}},'disable')"><abbr title="取消"><i class="fa fa-times"  ></i></button>
														{% elif ds.order_status == 8 %}
															<button  type="button" class="btn btn-default disabled"><abbr title="授权"><i class="fa fa-check"></i></button>	
															<button  type="button" class="btn btn-default" onclick="updateOrderStatus(this,{{ds.id}},'disable')"><abbr title="取消"><i class="fa fa-times "  ></i></button>		
														{% elif ds.order_status == 5 %}
															<button  type="button" class="btn btn-default disabled"><abbr title="授权"><i class="fa fa-check"></i></button>	
															<button  type="button" class="btn btn-default disabled"><abbr title="取消"><i class="fa fa-times"></i></button>																			
														{% else %}
															<button  type="button" class="btn btn-default disabled"><abbr title="授权"><i class="fa fa-check"></i></button>
															<button  type="button" class="btn btn-default" onclick="updateOrderStatus(this,{{ds.id}},'disable')"><abbr title="取消"><i class="fa fa-times">  </i></button>	
														{% endif %}											
													{% else %}
														<button  type="button" class="btn btn-default disabled"><abbr title="执行SQL"><i class="fa fa-play-circle-o"></i></button>
														<button  type="button" class="btn btn-default disabled"><abbr title="授权"><i class="fa fa-check" ></i></button>
														<button  type="button" class="btn btn-default disabled"><abbr title="取消"><i class="fa fa-times "></i></button>															
													{% endif %}
                                				{% endif %}
                                			</td>
                                		</tr>
                                	{% endfor %}
                                </tbody>
                            </table>
                            <!-- /.table-responsive -->
										{% if orderList %}
											<ul class="pagination">
											        {% if orderList.has_previous %}
											            <li><a href="/order/list/{{ projectList.previous_page_number }}/">&laquo;</a></li>
											        {% endif %}
											        {% for page in orderList.paginator.page_range %}
											        	{% if orderList.number == page %}
											        		<li class="disabled"><a href="/order/list/{{page}}/">{{page}}</a></li>
											        	{% else %}
											        		<li><a href="/order/list/{{page}}/">{{page}}</a></li>
											        	{% endif %}
											        {% endfor %}
											        {% if orderList.has_next %}
											            <li><a href="/order/list/{{ orderList.next_page_number }}/">&raquo;</a></li>
											        {% endif %}
											</ul>
                     		            {% endif %}	  
                        </div>
                        <!-- /.panel-body -->
                    </div>
                    <!-- /.panel -->
                </div>
    </div>  
<script type="text/javascript">

    
    $(document).ready(function() {
        $('#deployTableList').DataTable({
        	responsive: true,
        	"order": [[ 0, 'desc' ], [ 1, 'desc' ]]
        });
    } );    
       
    
	function deleteOrder(obj,id){
		var txt=  "是否确认删除？";
		var btnObj = $(obj);
		btnObj.attr('disabled',true);
		var option = {
			title: "工单"+id,
			btn: parseInt("0011",2),
			onOk: function(){
				$.ajax({
					  dataType: "JSON",
					  type: 'DELETE',
					  url: '/api/orders/'+id+'/',
				      success:function(response){	
				    	btnObj.removeAttr('disabled');
	                	window.wxc.xcConfirm("删除成功！", window.wxc.xcConfirm.typeEnum.success);
	                	location.reload();				            
				      },
		              error:function(response){
		            	btnObj.removeAttr('disabled');
		            	window.wxc.xcConfirm("删除失败！", window.wxc.xcConfirm.typeEnum.error);	
		              }
					});
			},
			onCancel:function(){	
			},
			onClose:function(){
			}
		}
		window.wxc.xcConfirm(txt, "custom", option);			   
	}    
	
	function updateOrderStatus(obj,id,model){ 
		if (model == 'auth' ){
			if (model == 'auth'){
				var txt=  "是否确认授权？";
				var title = "工单授权";
			}
			var option = {
				title: title,
				btn: parseInt("0011",2),
				onOk: function(){
					$.ajax({
						  dataType: "JSON",
						  type: 'PUT',
						  url: '/api/orders/'+id+'/',
						  data: {
							  "order_status":8
						  },
					      success:function(response){			            
				            window.wxc.xcConfirm("授权成功", window.wxc.xcConfirm.typeEnum.success);
				            location.reload();
		                
						},
			            error:function(response){
			            	window.wxc.xcConfirm("授权失败！", window.wxc.xcConfirm.typeEnum.error);
			            },							  
						});
				},
				onCancel:function(){	
				},
				onClose:function(){
				}
			}
			window.wxc.xcConfirm(txt, "custom", option);				
		} 		
		else if (model == 'disable'){
			window.wxc.xcConfirm("撤销原因：", window.wxc.xcConfirm.typeEnum.input,{
				onOk:function(result){
					if (result.length == 0){
						/* 如果没有输入字符串则直接退出 */
						return;
					};
					$.ajax({
						  dataType: "JSON",
						  type: 'PUT',
						  url: '/api/orders/'+id+'/',
						  data: {
							  'order_cancel':result,
							  "order_status":7
						  },
					     success:function(response){	
				            window.wxc.xcConfirm("撤销成功", window.wxc.xcConfirm.typeEnum.success);
				            location.reload();
						},
			            error:function(response){
			            	btnObj.removeAttr('disabled');
			            	window.wxc.xcConfirm("服务器响应错误，撤销失败", window.wxc.xcConfirm.typeEnum.error);
			            },							  
						});
					
				}
			})			
		}
   }

    Morris.Bar({
        element: 'morris-area-chart-code',
        data: [
               {% for ds in codeDataList %}
	               {
	            	  date:{{ds.date}},
	            	 {% for k,v in ds.items %}
	            	 	{% if k != 'date' %}
	            	 		{{k}}:'{{v}}',
	            	 	{% endif %}
	            	 {% endfor %}
	               },
               {% endfor %}
               ],
        xkey: 'date',
        ykeys: [
                	{% for ds in usernameList %}
                		"{{ds}}",
                	{% endfor %}
        		],
        labels: [
	             	{% for ds in usernameList %}
	        			"{{ds}}",
	        		{% endfor %}                
                 ],
        barColors:["#3c8dbc", "#f56954"], 
        pointSize: 2,
        hideHover: 'auto',
        resize: true
    });	
	
    Morris.Bar({
        element: 'morris-bar-chart-sql',
        data: [
               {% for ds in sqlDataList %}
	               {
	            	  date:{{ds.date}},
	            	 {% for k,v in ds.items %}
	            	 	{% if k != 'date' %}
	            	 		{{k}}:'{{v}}',
	            	 	{% endif %}
	            	 {% endfor %}
	               },
               {% endfor %}
               ],
        xkey: 'date',
        ykeys: [
                	{% for ds in usernameList %}
                		"{{ds}}",
                	{% endfor %}
        		],
        labels: [
	             	{% for ds in usernameList %}
	        			"{{ds}}",
	        		{% endfor %}                
                 ],
        barColors: ['#1E90FF', '#2E8B57'], 
        pointSize: 2,
        hideHover: 'auto',
        resize: true
    });	 
    
    Morris.Bar({
        element: 'morris-bar-chart-total',
        data: [
               {% for ds in monthDataList %}
               		{
	               		{% for k,v in ds.items %}
							{% if k == 'date' %}
								{{k}}:'{{v}}',
							{% else %}
								{{k}}:{{v}},
	                    	{% endif %}
	               		{% endfor %}
               		},
               {% endfor %}
		],
		barColors: ['#00a65a', '#f56954','#1E90FF','#778899'], 
        xkey: 'date',
        ykeys: ['sql','code','upload','download'],
        labels: ['sql','code','upload','download'],
        hideHover: 'auto',
        resize: true
    });	
	
    Morris.Donut({
        element: 'morris-donut-chart',
        data: [
               {% for status in statusList %}
               		{% for k,v in status.items %}
                  	 	{
                    		label: "{{k}}", 
                    		value: {{v}}
                    	 },               		
               		{% endfor %}
               {% endfor %}
			   ],
        resize: true
    });
    
</script>
{% endblock %}